#include <fstream>
#include <iostream>
#include <string>

#include "LinkedList.h"

using namespace std;

ListNode* processCommand( ifstream& inFile, char command, ListNode* pHead );

void main()
{
	ListNode* pHead = NULL;

	ifstream inFile( "ListDriver.txt" );

	if( inFile.good() )
	{
		while( !inFile.eof() )
		{
			//read a command
			char command;
			inFile >> command;
			pHead = processCommand( inFile, command, pHead );
		}
	}
	else
	{
		cout << "File not found!\n";
	}
	system( "pause" );
}

ListNode* processCommand( ifstream& inFile, char command, ListNode* pHead )
{
	switch( command )
	{
	case 'A'://add a word
		{
			//read a word
			string word;
			inFile >> word;

			//read a position
			int pos;
			inFile >> pos;

			//create a node
			ListNode* pNode = new ListNode( word );
			//add the node to the list in 'pos' position
			pHead = addNode( pHead, pNode, pos );

			//confirm
			cout << "word:" << word << " added at pos:" << pos << endl;
		}
		break;
	case 'R'://remove a word
		{
			//read a position
			int pos;
			inFile >> pos;

			//remove the node
			pHead = removeNode( pHead, pos );

			//confirm
			cout << "Node removed from pos:" << pos << endl;
		}
		break;
	case 'D'://delete all nodes
		{
			pHead = removeAllNodes( pHead );

			//confirm
			cout << "All Nodes removed!" << endl;

		}
		break;
	case 'P'://print report of all nodes
		{
			cout << "Nodes:" << getNodeReport( pHead ) << endl;
		}
		break;
	default:
		cout << "Shouldn't get here!\n\n";
	}
	return pHead;
}
